package com.ggy.babybed.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;

import com.ggy.babybed.R;
import com.ggy.babybed.network.NetWorkManager;
import com.ggy.babybed.view.MessageView;
import com.ggy.babybed.wilddog.WildDogDataChange;
import com.ggy.babybed.wilddog.WildDogNode;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public class MessageActivity extends AppCompatActivity {
    public static final String TAG = "MessageActivity";
    private MessageView lineChart;
    private Toolbar mToolbar;
    private Map<String, Long> mWenduData = new HashMap<>();
    private Map<String, Long> mShiduData = new HashMap<>();
    private ListenforWenduShidu mListenWendu;
    private ListenforWenduShidu mListenShidu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        mToolbar = (Toolbar)findViewById(R.id.toolbar);

        mToolbar.setTitle("");
        mToolbar.setSubtitle("温度/湿度");
        mToolbar.setSubtitleTextColor(getResources().getColor(R.color.white));
        mToolbar.setNavigationIcon(R.mipmap.icon_back);
        setSupportActionBar(mToolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        lineChart = (MessageView) findViewById(R.id.line_chart);
        mListenWendu = new ListenforWenduShidu(mWenduData);
        mListenShidu = new ListenforWenduShidu(mShiduData);
        NetWorkManager.getNetWorkManager(this).addListenForNode(WildDogNode.WENDU_HISTORY_NODE, mListenWendu);
        NetWorkManager.getNetWorkManager(this).addListenForNode(WildDogNode.SHIDU_HISTORY_NODE, mListenShidu);
       // initChart();
    }

    public void wenduClick(View view) {
     //   showChart(handlerData(mWenduData), "℃");
    }

    public void shiduClick(View view) {
     //   showChart(handlerData(mShiduData), "%RH");
    }

    private LineData handlerData(Map<String, Long> datas) {
        ArrayList<Entry> vals = new ArrayList<>();
        String[] xvals = {"00:00", "02:00", "04:00", "06:00", "08:00", "10:00", "12:00",
                "14:00", "16:00", "18:00", "20:00", "22:00"};
        int i = 0;
        for (String s : datas.keySet()) {
            if (i == xvals.length)
                break;
            Entry entry = new Entry(datas.get(s), i, s);
            vals.add(entry);
            i++;
        }
        LineDataSet lds = new LineDataSet(vals, DateFormat.format("yyyy.MM.dd", Calendar.getInstance().getTime()).toString());
        lds.setColors(ColorTemplate.VORDIPLOM_COLORS);
        lds.setCircleSize(3f);
        lds.setLineWidth(1.75f);
        lds.setColor(Color.WHITE);
        lds.setCircleColor(Color.WHITE);
        LineData lineData = new LineData(xvals, lds);
        return lineData;
    }

    private class ListenforWenduShidu implements  WildDogDataChange {
        private Map<String, Long> datas;

        public ListenforWenduShidu(Map<String, Long> datas){
            this.datas = datas;
        }

        @Override
        public void onchange(Collection data){
            for(Object obj : data){
                if(obj instanceof Map){
                    Object tmp = ((Map)obj).get("current");
                    if(tmp != null){
                        Integer.valueOf(tmp.toString()); //current
                    }else{
                        datas = (Map)obj;
                        break;
                    }
                }
            }
        }
    }

    //add by sizi for set wendu and shidu data at 2016-6-2
    private void setWenduData(Map<String, Long> wenduData){
        this.mWenduData=wenduData;
    }

    private void setShidData(Map<String, Long> shidu){
        this.mShiduData=shidu;
    }

    @Override
    protected void onDestroy() {
        Log.i(TAG, "on destory!");
        NetWorkManager.getNetWorkManager(this).rmListenForNode(WildDogNode.WENDU_HISTORY_NODE, mListenWendu);
        NetWorkManager.getNetWorkManager(this).rmListenForNode(WildDogNode.SHIDU_HISTORY_NODE, mListenShidu);
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        Log.i(TAG, "on stop!");
        super.onStop();
    }
}
